<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * 商城地址统一管理
 */
namespace Addon\Mall\Controller;
use Addon\Mall\Controller\CommonController;

/**
 * 商城地址统一管理
 */
class AddressController extends CommonController {
    
	//地址管理
	public function Manage(){
		
		//实例化商城内容表
		$m_mall_content = M('mall_content');
		
		//地址条件
		$map['uid'] = getUID();
		$map['form_id'] = 0;
		$map['type'] = get_mall_model_conf('MALL_USER_ADDRESS');
		$map['status'] = 1;

		//获得地址
		$address = $m_mall_content->where($map)->order('sort desc')->select();
	
		$this->assign('address',$address);
		
		//获得微信信息 共享微信地址
		$data = array();
		$data["appid"] = get_mall_order_conf('MALL_WEIXIN_APPID');
		$data["url"] = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$time = time();
		$data["timestamp"] = "$time";
		$data["noncestr"] = "1234568";
		$accesstoken = \Org\Net\Wechat\Oauth::token(get_mall_order_conf('MALL_WEIXIN_APPID'),get_mall_order_conf('MALL_WEIXIN_APPSECRET'));
		
		
		$data["accesstoken"] = $accesstoken['access_token'];
		ksort($data);
		$params = $this->ToUrlParams($data);
		$addrSign = sha1($params);
		
		$afterData = array(
			"addrSign" => $addrSign,
			"signType" => "sha1",
			"scope" => "jsapi_address",
			"appId" => get_mall_order_conf('MALL_WEIXIN_APPID'),
			"timeStamp" => $data["timestamp"],
			"nonceStr" => $data["noncestr"]
		);
		$parameters = json_encode($afterData);

		$this->assign('parameters',$parameters);
		
		
		$top_url = get_mall_order_conf('MALL_ADDRESS_TOP_URL');
		$return_url = get_mall_order_conf('MALL_ADDRESS_RETURN_URL');
		
		$this->assign('top_url',$top_url);
		$this->assign('return_url',$return_url);
		
		
		$this->view();
	}
	
	 /**
	 * 
	 * 拼接签名字符串
	 * @param array $urlObj
	 * 
	 * @return 返回已经拼接好的字符串
	 */
	private function ToUrlParams($urlObj)
	{
		$buff = "";
		foreach ($urlObj as $k => $v)
		{
			if($k != "sign"){
				$buff .= $k . "=" . $v . "&";
			}
		}
		
		$buff = trim($buff, "&");
		return $buff;
	}
	
	//商城公告
	public function Create(){
		
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_mall_content = M('mall_content');
			
			$dmap['uid'] = getUID();
			$dmap['type'] = get_mall_model_conf('MALL_USER_ADDRESS');
			$dmap['status'] = 1;
			
			$ddata['sort'] = 0;
			
			$result = $m_mall_content->where($dmap)->save($ddata);
			
			//获得数据
			$data = I();
			
			$data['uid'] = getUID();
			$data['type'] = get_mall_model_conf('MALL_USER_ADDRESS');
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['sort'] = 1;
			$data['status'] = 1;
			
			$result = $m_mall_content->add($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
			
			
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	//默认地址
	public function Priority(){
		
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_mall_content = M('mall_content');
			
			
			
			$map['uid'] = getUID();
			$map['type'] = get_mall_model_conf('MALL_USER_ADDRESS');
			$map['status'] = 1;
			
			$data['sort'] = 0;
			
			$result = $m_mall_content->where($map)->save($data);
			
			
			$smap['id'] = I('param.id');
			$smap['uid'] = getUID();
			$smap['type'] = get_mall_model_conf('MALL_USER_ADDRESS');
			$smap['status'] = 1;
			
			$sdata['sort'] = 1;
			
			$result = $m_mall_content->where($smap)->save($sdata);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	//删除地址
	public function Delete(){
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_mall_content = M('mall_content');
			
			
			$map['id'] = I('param.id');
			$map['uid'] = getUID();
			$map['type'] = get_mall_model_conf('MALL_USER_ADDRESS');
			
			$data['status'] = 2;
			
			$result = $m_mall_content->where($map)->save($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
	
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	
}